/**************************************************************************************************
 * light library                                                                                  *
 * Copyright © 2012 David Kretzmer                                                                *
 *                                                                                                *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software  *
 * and associated documentation files (the "Software"), to deal in the Software without           *
 * restriction,including without limitation the rights to use, copy, modify, merge, publish,      *
 * distribute,sublicense, and/or sell copies of the Software, and to permit persons to whom the   *
 * Software is furnished to do so, subject to the following conditions:                           *
 *                                                                                                *
 * The above copyright notice and this permission notice shall be included in all copies or       *
 * substantial portions of the Software.                                                          *
 *                                                                                                *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING  *
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND     *
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.        *
 *                                                                                                *
 *************************************************************************************************/

#pragma once

#include <light/utility/type_set.hpp>


namespace light
{
namespace utility
{

	namespace internal_array_set
	{
		struct push_back
		{
			template<typename Container, typename Arg>
			void operator () (Container &t, Arg const &arg)
			{
				t.push_back(arg);
			}
		};

		template<typename ...ValueTypes>
		struct get_at_index
		{
			get_at_index(type_set<ValueTypes...> *target, size_t i) :
				m_index(i),
				m_set(target) {}

			template<typename T>
			void operator () (T &t)
			{
				get<typename T::value_type>(*m_set) = t[m_index];
			}

			size_t m_index;
			type_set<ValueTypes...> *m_set;
		};
	}


	template<typename ...T>
	void pass(T...) {}


	//=============================================================================================
	//
	//=============================================================================================
	template<typename ...ValueTypes>
	class array_set
	{
	public:

		template<typename T>
		using vector_type = std::vector<T>;

		void add(ValueTypes const &...vals)
		{
			for_each_arg(m_data, internal_array_set::push_back(), vals...);
		}

		void insert(size_t pos, size_t num, ValueTypes const * ...vals)
		{
			pass(
				(array<ValueTypes>().insert(array<ValueTypes>().begin() + pos, vals, vals + num), 1
				)...
			);
		}

		type_set<ValueTypes...> at(size_t i) const
		{
			type_set<ValueTypes...> result;
			internal_array_set::get_at_index<ValueTypes...> get(&result, i);
			for_each(m_data, get);

			return result;
		}

		size_t size() const { return m_data.m_value.size(); }

		template<typename Type>
		std::vector<Type>& array()
		{
			return get<std::vector<Type>>(m_data);
		}

		template<typename Type>
		std::vector<Type> const& array() const
		{
			return get_const<std::vector<Type>>(m_data);
		}

		type_set<std::vector<ValueTypes>...> m_data;
	};

} // namespace: utility

using utility::array_set;

} // namespace: light
